package com.lql.lqlreader.service

import android.content.Context
import android.os.Environment
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.lql.lqlreader.service.converter.MyGsonConverterFactory
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import timber.log.Timber
import java.io.File
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit


/**
 * ClassName:   ServiceFactory
 * Description: The factory class mainly offers two kinds of Retrofit services, Rx mode and normal mode
 *
 * Date:        2021/6/19
 */
object ServiceFactory {
    private const val DEFAULT_TIMEOUT: Long = 10
    lateinit var DEFAULT_CONTEXT: WeakReference<Context> // 此变量在LQlReaderApplication.kt中初始化

    private val loggingInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger {
        Timber.tag("HttpLogging\$ Start")
        Timber.d(" ┌────────────────────────────────────────────────────────────────────")
        Timber.tag("HttpLogging\$ Result")
        Timber.d("| %s", it)
        Timber.tag("HttpLogging\$ TheEnd")
        Timber.d("└────────────────────────────────────────────────────────────────────")
    })

    private fun getOkHttpClient(): OkHttpClient { // 记录日志
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
        val builder = OkHttpClient.Builder()
        builder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
        builder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
        builder.addInterceptor(loggingInterceptor)
        builder.cookieJar(PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(DEFAULT_CONTEXT.get())))
        builder.cache(Cache(File(Environment.getExternalStorageDirectory(), "RsKotlin"), 10 * 1024 * 1024))
        return builder.build()
    }

    fun <T> createRxRetrofitService(clazz: Class<T>, endPoint: String): T {
        val retrofit = Retrofit.Builder()
                .baseUrl(endPoint)
                .client(getOkHttpClient())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(MyGsonConverterFactory.create())
                .build()
        return retrofit.create(clazz)
    }

    fun <T> createRetrofitService(clazz: Class<T>, endPoint: String): T {
        return Retrofit
                .Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(endPoint)
                .build()
                .create(clazz)
    }
}